package com.xiaoxu.media.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.base.exception.bkSchoolException;
import com.xiaoxu.media.dao.MediaFilesMapper;
import com.xiaoxu.media.dao.MediaProcessHistoryMapper;
import com.xiaoxu.media.dao.MediaProcessMapper;
import com.xiaoxu.media.model.po.MediaFiles;
import com.xiaoxu.media.model.po.MediaProcess;
import com.xiaoxu.media.model.po.MediaProcessHistory;
import com.xiaoxu.media.service.MediaProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xiaoxu
 * @version 1.0
 * @description TODO
 * @date 2023/1/25 15:59
 */
@Service
@Slf4j
public class MediaProcessServiceImpl extends ServiceImpl<MediaProcessMapper, MediaProcess> implements MediaProcessService {
    private final MediaFilesMapper mediaFilesMapper;
    private final MediaProcessMapper mediaProcessMapper;
    private final MediaProcessHistoryMapper mediaProcessHistoryMapper;


    public MediaProcessServiceImpl(MediaFilesMapper mediaFilesMapper, MediaProcessMapper mediaProcessMapper, MediaProcessHistoryMapper mediaProcessHistoryMapper) {
        this.mediaFilesMapper = mediaFilesMapper;
        this.mediaProcessMapper = mediaProcessMapper;
        this.mediaProcessHistoryMapper = mediaProcessHistoryMapper;
    }

    /**
     * @param shardIndex 分片序号
     * @param shardTotal 分片总数
     * @param count      获取记录数
     * @return List<MediaProcess>
     * @description 获取待处理任务
     */
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
    }

    /**
     * @param taskId   任务id
     * @param status   任务状态
     * @param fileId   文件id
     * @param url      url
     * @param errorMsg 错误信息
     * @description 保存任务结果
     */
    @Override
    @Transactional
    public void updateProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //查出任务，如果不存在则直接返回
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (mediaProcess == null) {
            return;
        }
        //处理失败，更新任务处理结果
        if ("3".equals(status)) {
            mediaProcess.setStatus("3");
            mediaProcess.setErrorMsg(errorMsg);
            int count = mediaProcessMapper.updateById(mediaProcess);
            if (count <= 0){
                throw new bkSchoolException("mediaProcessMapper 更新信息失败");
            }
            return;
        }
        //处理成功，更新原文件保存url
        LambdaQueryWrapper<MediaFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MediaFiles::getFileId,fileId);
        MediaFiles mediaFiles = mediaFilesMapper.selectOne(queryWrapper);
        if (mediaFiles != null) {
            //更新处理之后的视频url
            mediaFiles.setUrl(url);
            int count = mediaFilesMapper.updateById(mediaFiles);
            if (count <= 0){
                throw new bkSchoolException("mediaFilesMapper 更新信息失败");
            }
        }
        //处理成功，更新url和状态
        mediaProcess.setUrl(url);
        //状态 2代表处理成功
        mediaProcess.setStatus("2");
        mediaProcess.setFinishDate(LocalDateTime.now());
        int count = mediaProcessMapper.updateById(mediaProcess);
        if (count <= 0){
            throw new bkSchoolException("mediaProcessMapper 更新信息失败");
        }

        //添加到历史记录
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
        int insertCount = mediaProcessHistoryMapper.insert(mediaProcessHistory);
        if (insertCount<=0){
            throw new bkSchoolException("mediaProcessHistoryMapper 插入数据失败");
        }
        //删除mediaProcess
        int deleteCount = mediaProcessMapper.deleteById(mediaProcess.getId());
        if (deleteCount <= 0 ){
            throw new bkSchoolException("mediaProcessMapper 删除数据失败");
        }

    }

}
